﻿/*
 * Created by SharpDevelop.
 * User: Zoila
 * Date: 26/11/2011
 * Time: 06:28 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Web;
using Business_Logic;
using Business_Entity;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace PetSoft.GUI.Ventas
{
	/// <summary>
	/// Description of GUIMostrarDocVenta.
	/// </summary>
	public partial class GUIMostrarDocVenta : Form
	{
		BEDocumentoVenta documento= new BEDocumentoVenta();
		BLCaja blcaja = new BLCaja();
		List<String []>  lista = new List<string[]>();
		public GUIMostrarDocVenta()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		public GUIMostrarDocVenta(BEPedidoVenta pedidoPag):this()
		{
			try{
				documento= blcaja.gedDocumentoVenta(pedidoPag);
				string nomReport = "DocumentoVenta "+pedidoPag.Id.ToString();
				int cont = 1;
				String path = "..\\..\\"+nomReport +".pdf";
				while (true)
				{
					if (File.Exists(path))
					{
						path = "..\\..\\"+nomReport +"-"+ cont.ToString() + ".pdf";
						cont++;
					}
					else
					{
						break;
					}
				}
				Document doc = new Document(PageSize.A4.Rotate());
				doc.AddCreationDate();
				PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));
				doc.Open();
				iTextSharp.text.Font fntTit = FontFactory.GetFont(FontFactory.COURIER_BOLD, 20, BaseColor.RED);
				iTextSharp.text.Font fntTab = FontFactory.GetFont(FontFactory.COURIER, 8, BaseColor.BLUE);
				iTextSharp.text.Font fntRes = FontFactory.GetFont(FontFactory.COURIER, 7);
				PdfPTable tab = new PdfPTable(8);
				tab.TotalWidth = 820f;
				float[] widths = new float[] { 40f, 90f, 90f, 90f, 90f, 90f, 90f, 90f};
				tab.SetWidths(widths);
				tab.LockedWidth = true;
				PdfPCell cell = new PdfPCell(new Phrase(BLInfo.razonSocial,fntTit));
				cell.Colspan = 8;
				cell.HorizontalAlignment = 1;
				tab.AddCell(cell);
				PdfPCell cell2 = new PdfPCell(new Phrase("Descripcion",fntTab));
				cell2.HorizontalAlignment = Element.ALIGN_CENTER;
				cell2.VerticalAlignment = Element.ALIGN_CENTER;
				tab.AddCell(cell2);
				
				cell2 = new PdfPCell(new Phrase("Precio Unitario",fntTab));
				cell2.HorizontalAlignment = Element.ALIGN_CENTER;
				cell2.VerticalAlignment = Element.ALIGN_CENTER;
				tab.AddCell(cell2);
				
				cell2 = new PdfPCell(new Phrase("Cantidad",fntTab));
				cell2.HorizontalAlignment = Element.ALIGN_CENTER;
				cell2.VerticalAlignment = Element.ALIGN_CENTER;
				tab.AddCell(cell2);
				
				cell2 = new PdfPCell(new Phrase("Precio",fntTab));
				cell2.HorizontalAlignment = Element.ALIGN_CENTER;
				cell2.VerticalAlignment = Element.ALIGN_CENTER;
				tab.AddCell(cell2);
				
				llenaLista();
				
				foreach (string[] fila in lista) {
						cell2 = new PdfPCell(new Phrase(fila[0],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[1],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[2],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						/*cell2 = new PdfPCell(new Phrase(fila[3],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);*/
						
					}
					doc.Add(tab);
					doc.Close();
//					MessageBox.Show("Exportación exitosa", "Aviso");
					System.Diagnostics.Process.Start("AcroRd32.exe",path);
				//	this.Close();
				
			}
			catch (Exception ex)
			{
				MessageBox.Show("Exportación fallida: " + ex,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
		
		
		private void llenaLista(){
			foreach (BELineaDocumentoVenta doc in documento.Lineas){
				String[] row0={doc.Descripcion,Math.Round(doc.PrecioUnitario,3).ToString(),doc.Cantidad.ToString()};
				this.lista.Add(row0);
			}
		}
		
	}
}
